package com.woniu.controller;

import com.google.code.kaptcha.Producer;
import com.woniu.Utils.*;
import com.woniu.entity.User;
import com.woniu.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping("/login")
    public Map<String,String> login(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) {
        System.out.println(name);
        Integer result = userService.login(name, password);
        if (result == 0) {
            Map<String,String> map = new HashMap<>();
            map.put("error","用户名不存在");
            return map ;
        } else if (result == 1) {
            Map<String,String> map = new HashMap<>();
            map.put("error","密码错误");
            return map ;
        } else {
            String token = JwtUtils.createToken(name,5);
            String uuid= UUID.randomUUID().toString().replace("-", "").toUpperCase();
            Map<String,String> map = new HashMap<>();
            map.put("token",token);
            map.put("account",name);
            map.put("uuid",uuid);
            System.out.println(map);
            return map;

        }
    }

    @RequestMapping("/register")
    public String register(@RequestParam(value = "username") String username,@RequestParam(value = "password") String password,@RequestParam("tel") String tel,@RequestParam("emailCode") String emailCode,@RequestParam("email") String email) throws IOException {
        String realCode = stringRedisTemplate.opsForValue().get(email);
        if(StringUtils.isEmpty(realCode)||!realCode.equals(emailCode)){
            return "验证码错误";
        }
        //检查用户名，电话，邮箱是否重复
        String confirmResult= userService.confirmExist(username,tel,email);
        if(confirmResult!=null){
            return confirmResult;
        }
        //插入数据
        IdWorker idWorker = new IdWorker(1,1,8);
        User user=new User();
        Long id= idWorker.nextId();
        user.setId(id);
        user.setName(username);
        user.setPassword(password);
        user.setTelephone(tel);
        user.setEmail(email);
        user.setImgs("http://192.168.201.121:10002/imgs/woniu.jpg");
        Date createTime = GetNetWorkTime.getTime();
        user.setCreateTime(createTime);
        user.setLastModifidTime(createTime);
        user.setStatus(0);
        Integer result=userService.register(user);
        if(result==1){
            return "ok";
        }else {
            return "fail";
        }
    }

    @RequestMapping("/sendEmail")
    public String sendEmail(@RequestParam("email") String email){
        String checkEmail = stringRedisTemplate.opsForValue().get(email);
        String emailReg="^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$";
        if(!email.matches(emailReg)){
            return "请检查你的邮箱格式";
        }else if(checkEmail==null){
            rabbitTemplate.convertAndSend("sendEmailExchange","sendEmail",email);
            return "ok";
        }else {
            return "请勿重复提交";
        }
    }

    @RequestMapping("/getUserByName")
    public String getUserByName(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        return "proj/homeh";
    }

    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        return "/";
    }


}
